package com.yupi.yupicturebackend.controller;

import com.yupi.yupicturebackend.utils.CaptchaGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Base64;

@RestController
public class CaptchaController {

    @GetMapping("/captcha")
    public ResponseEntity<String> getCaptcha(HttpSession session) throws IOException {
        String captchaText = CaptchaGenerator.generateCaptchaText();
        byte[] captchaImage = CaptchaGenerator.getCaptchaImageBytes(captchaText);

        // 将图像转换为 Base64 字符串
        String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(captchaImage);

        System.out.println("1213");
        // 保存验证码文本到 session 中
        session.setAttribute("captcha", captchaText);

        return ResponseEntity.ok(base64Image);
    }
}
